using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using System.Data.SqlClient;

namespace Fieldbook.Common
{
    public static class Utilities
    {
        #region Database utilities
        public static SqlDatabase GetDatabase()
        {
            SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
            return db;
        }

        public static DbTransaction BeginTransaction(SqlDatabase db)
        {
            DbConnection conn = db.CreateConnection();
            conn.Open();
            DbTransaction transaction = conn.BeginTransaction();
            return transaction;
        }

        public static void EndTransaction(DbTransaction transaction)
        {
            if (transaction != null)
            {
                if (transaction.Connection != null && transaction.Connection.State == System.Data.ConnectionState.Open)
                {
                    transaction.Connection.Close();
                }
                transaction.Dispose();
            }
        }
        #endregion
    }
}
